package com.xr.org.emp;

import java.util.List;

import com.bstek.dorado.common.*;
import com.bstek.dorado.data.*;
import com.bstek.dorado.data.db.DBStatement;
import com.bstek.dorado.data.db.SqlDataset;
import com.bstek.dorado.utils.variant.VariantSet;
import com.bstek.dorado.view.*;
import com.bstek.dorado.view.data.*;
import com.bstek.dorado.view.control.*;

/**
 * subQuerySelectViewModel
 */
public class subQuerySelectViewModel extends DefaultViewModel {
  protected void doUpdateData(ParameterSet parameters, ParameterSet outParameters)
      throws Exception {
    // Add your code here 
    super.doUpdateData(parameters, outParameters);
    
  }
  public void delQuery(ParameterSet parameters, ParameterSet outParameters)
  throws Exception {
	  DBStatement dbs = new DBStatement();
		Dataset ds = getDataset("datasetQuery");
		dbs.setSql("delete from EMP_QUERY_SQL where emp_query_id="+ds.getString("EMP_QUERY_ID"));
		dbs.execute();
		dbs.setSql("delete from EMP_QUERY where emp_query_id="+ds.getString("EMP_QUERY_ID"));
		dbs.execute();
		dbs.close();

}
  public void yanzhenSql(ParameterSet parameters, ParameterSet outParameters)
  throws Exception {
  String sql="",sqlCon="";
  Dataset ds=getDataset("datasetQuery");
  String queryId=ds.getString("EMP_QUERY_ID");
  DBStatement dbs = new DBStatement(); 
dbs.setSql("select * from emp_query_sql where emp_query_id="+queryId+" order by sx");
  List cl=dbs.queryForList();
  for(int i=0;i<cl.size();i++){
	  VariantSet vs=(VariantSet)cl.get(i);
	  sqlCon+=""+toChar(vs.getString("LEFT_BRACKET"))+" exists(select 1 from "+vs.getString("table_name")+
	  " where emp.employee_id="+vs.getString("table_name")+".employee_id and nvl("+
	  vs.getString("table_name")+"."+vs.getString("field_name")+",' ') "+vs.getString("MATCHAR")+" '"+vs.getString("field_value")+"') "+toChar(vs.getString("RIGHT_BRACKET"))+" "+toChar(vs.getString("LOGICHAR"))+" ";
	  
  } 
  
  if(sqlCon.equals("")){
	  sqlCon=" 1=1";
  }
  sql="select 1 from org_employee emp where 1=1 and "+sqlCon;
  dbs.setSql(sql);
  dbs.close();
  
  }
  private String toChar(String d){
		if(d==null){
			return "";
		}else{
			return d;
		}
	}
}
